package com.qining618.cms.cms.service.impl;

import com.qining618.cms.cms.domain.Sensitivity;
import com.qining618.cms.cms.mapper.SensitivityMapper;
import com.qining618.cms.cms.service.SensitivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qining618.common.utils.StringUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 敏感词 服务实现类
 * </p>
 *
 * @author qining
 * @date 2019-09-19
 */
@CacheConfig(cacheNames = "sensitivity")
@Transactional(readOnly = true, rollbackFor = Exception.class)
@Service
public class SensitivityServiceImpl extends ServiceImpl<SensitivityMapper, Sensitivity> implements SensitivityService {

    @Override
    public String replaceSensitivity(String str) {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        List<Sensitivity> list = this.list();
        for (Sensitivity sen : list) {
            str = StringUtils.replace(str, sen.getSearch(), sen.getReplacement());
        }
        return str;
    }

    @Cacheable(key = "targetClass + methodName")
    @Override
    public List<Sensitivity> list() {
        return super.list();
    }
}
